package com.wandian.weather.mvvm.weatheractivity;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.weather.bean.WeatherInformationBean;
import com.wandian.weather.data.BaseAppApi;
import com.wandian.weather.tools.JmTools;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author Administrator
 * @date 2019/12/16-13:32
 * @description FragmentHomeViewModel
 **/
public class WeatherHomeActivityViewModel extends BaseViewModel {

    public MutableLiveData<WeatherInformationBean> weatherInformationBean = new MutableLiveData<>();

    public WeatherHomeActivityViewModel(@NonNull Application application) {
        super(application);
    }
    //国内天气
    public void getWeatherData(Context mContext){
        //创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.tianqijun.com/") //基础url,其他部分在GetRequestInterface里
                .addConverterFactory(GsonConverterFactory.create()) //Gson数据转换器
                .build();
        //创建网络请求接口实例
        BaseAppApi request = retrofit.create(BaseAppApi.class);
        HashMap<String, String> hashMap = new HashMap<>();
        //其他参数
        hashMap.put("service","Tianqi.Index");
        hashMap.put("lng","106.578702");
        hashMap.put("air","1");
//        hashMap.put("cityid","101210415");
        hashMap.put("hour24","1");
        hashMap.put("weather30","15");
        hashMap.put("yesterday","1");
        hashMap.put("outline","1");
        hashMap.put("precipitation_2h","1");
        hashMap.put("lat","29.617165");
        hashMap.put("need_city","1");
        hashMap.put("lifeindex","1");
        hashMap.put("nl","1");
        hashMap.put("wind","1");
        //公用参数
        hashMap.put("os","1");
        hashMap.put("pid","4");
        hashMap.put("version","6.5.4");

        Call<Object> call = request.getWeatherData(JmTools.encryptParams(hashMap, "Tianqi.Index"));
        //发送网络请求(异步)
        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {

                if(UIUtils.isEmpty(response.body())){
                    return;
                }
                Log.d("getWeatherData", "getWeatherData="+ JsonUtil.toJson(response.body()));
                try {
                    JSONObject jsonObject = new JSONObject(JsonUtil.toJson(response.body()));
                    WeatherInformationBean informationBean =(WeatherInformationBean) JsonUtil.parseJsonToBean(jsonObject.optString("data"), WeatherInformationBean.class);
                    if(!UIUtils.isEmpty(informationBean)){
                        weatherInformationBean.setValue(informationBean);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                Log.d("getWeatherData", "error="+t.getMessage());
            }
        });
    }
}
